import os

from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.chat_models import init_chat_model
from langchain.tools import tool
# from langchain.llms import OpenAI
# llm = OpenAI(temperature=0)

os.environ["LANGSMITH_TRACING"] = "true"
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass()
os.environ["LANGSMITH_API_KEY"] = "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"

os.environ["SERPAPI_API_KEY"] = "true"

llm = init_chat_model(
    model_provider="openai",
    model="qwen/qwq-32b:free",
    base_url="https://openrouter.ai/api/v1",
    api_key="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",
)


@tool
def search_api(query: str) -> str:
    """查询可用api"""

    # 使用传入的query参数调用大模型
    response = llm.invoke("你是谁")
    return f"大模型返回的结果: {response.content}"


# tools = load_tools(["search_api", "llm-math"], llm=llm)
tools = [
    search_api,
]

agent = initialize_agent(
    tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)


agent.invoke("计算 1 + 3 的结果")
